package com.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.house.entity.SystemUser;
import com.house.enums.BaseStatus;
import com.house.exception.LeaseException;
import com.house.mapper.SystemUserMapper;
import com.house.result.ResultCodeEnum;
import com.house.service.LoginService;
import com.house.service.SystemUserService;
import com.house.service.UserInfoService;
import com.house.utils.JwtUtil;
import com.house.vo.login.CaptchaVo;
import com.house.vo.login.LoginVo;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemUserMapper systemUserMapper;
    @Override
    public CaptchaVo getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String image = specCaptcha.toBase64();
        String value = specCaptcha.text().toLowerCase();
        String key = "admin:login:" + UUID.randomUUID();
        stringRedisTemplate.opsForValue().set(key,value,60, TimeUnit.SECONDS);

        return new CaptchaVo(image,key);
    }

    @Override
    public String login(LoginVo loginVo) {
        if (loginVo.getCaptchaKey()==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND.getMessage(),ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND.getCode());
        }
        String key = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if (key==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED.getMessage(),ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED.getCode());
        }
        if (!key.equals(loginVo.getCaptchaCode().toLowerCase())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR.getMessage(),ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR.getCode());
        }
        SystemUser systemUser = systemUserMapper.selectByinfo(loginVo.getUsername());
        if (systemUser==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR.getMessage(),ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR.getCode());
        }
        if (systemUser.getStatus()== BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR.getMessage(),ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR.getCode());
        }
        if (!systemUser.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR.getMessage(),ResultCodeEnum.ADMIN_ACCOUNT_ERROR.getCode());
        }

        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());
    }
}
